package cn.antia.cwin;

import cn.antia.cep.common.pool.AntiaPoolConst;
import cn.antia.cwin.grid.SingleGrid;
import cn.antia.cwin.grid.GridConfiger;
import cn.antia.cwin.window.SingleWindow;
import cn.antia.cwin.window.WindowConfiger;
import cn.antia.cwin.window.MultiWindow;
import cn.antia.cwin.db.DbService;

import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 * User: Jacob Xiao
 * Date: 2008-7-18
 * Time: 13:47:07
 */
public class ConfigPool {
	/**
	 * 存储的信息： t_表名:表结构--List:ElementComsat对象
	 */
    private static Map<String, Cele> commonElements;       //公用的FIELD定义
    private static Map<String, List<Cele>> dbConfigElements;		//数据表配置
    private static Map<String,List<Cele>> dbElements;               //数据表原始元素
    private static Map<String, SingleGrid> gridElements;			//表格配置元素
	private static Map<String, SingleWindow> singleWindows;				//window config
	private static Map<String, MultiWindow> multiWindows;			//multiWindows
    
	public static Map<String, List<Cele>> getDbConfigElements() {
		return dbConfigElements;
	}
	public static Map<String, SingleGrid> getGridElements() {
		return gridElements;
	}
    public static Map<String, Cele> getCommonElements() {
        return commonElements;
    }
    public  static Map<String, SingleWindow> getSingleWindows() {
		return singleWindows;
	}
    public static Map<String, List<Cele>> getDbElements() {
        return dbElements;
    }
    public static Map<String, MultiWindow> getMultiWindows() {
        return multiWindows;
    }

    /**
	 * 取得通信对象集合
	 *
	 * @param table
	 *            表名
	 * @return 通信对象列表
	 */
	public static List<Cele> getElementComsats(String table) {
		String key = "t_" + table;
		List<Cele> comsats;
		if (dbConfigElements == null) {
			dbConfigElements = new HashMap<String, List<Cele>>();
			comsats = DBConfiger.getTableElements(table);
			dbConfigElements.put(key, comsats);
		} else {
			comsats = dbConfigElements.get(key);
			if (comsats == null || comsats.size() < 1) {
				comsats = DBConfiger.getTableElements(table);
				dbConfigElements.put(key, comsats);
			}
		}
        return comsats;
    }
    
	public static  void resetAllComsats() {
		dbConfigElements = null;
	}
    
	/**
	 * 取得单表表格配置
	 * @param gridId grid id
	 * @return SingleGrid
	 */
	public static  SingleGrid getSingleGrid(String gridId){
		if(gridElements == null){
			gridElements = new HashMap<String, SingleGrid>();
		}
		String key = AntiaPoolConst.SINGLE_GRID_PREFIX + gridId;
		SingleGrid grid = gridElements.get(key);
		if(grid == null){
			grid = GridConfiger.getSingleGrid(gridId);
			gridElements.put(key, grid);
		}
		return grid;
	}
	/**
	 * 取得配置的操作窗口
	 * @param winId window id
	 * @return SingleWindow
	 */
	public static  SingleWindow getSingleWindow(String winId) {
		if(singleWindows == null){
			singleWindows = new HashMap<String, SingleWindow>();
		}
		SingleWindow window = singleWindows.get(winId);
		if(window == null){
			window = WindowConfiger.getSingleWindow(winId);
			singleWindows.put(winId, window);
		}
		return window;
	}
	/**
	 * 取得配置的操作窗口(多窗口)
	 * @param winId id
	 * @return MultiWindow
	 */
	public static  MultiWindow getMultiWindow(String winId) {
		if(multiWindows == null){
			multiWindows = new HashMap<String, MultiWindow>();
		}
		MultiWindow window = multiWindows.get(winId);
		if(window == null){
			window = WindowConfiger.getMultiWindow(winId);
			multiWindows.put(winId, window);
		}
		return window;
	}

    public static Cele getCommonField(String fieldName) {
        if (commonElements == null) {
            commonElements = DBConfiger.getCommonFields();
        }
        if (commonElements == null || commonElements.size() == 1) {
            return null;
        }
        return commonElements.get(fieldName);
    }
    
    public static List<Cele> getDbElements(String table) {
        if (dbElements == null) {
            dbElements = new HashMap<String, List<Cele>>();
        }
        if (dbElements.get(table) == null) {
            List<Cele> lc = new DbService().getAttribute(table);
            dbElements.put(table, lc);
        }
        return dbElements.get(table);
    }
}
